package com.lib.framework.ext

import android.util.Log
import java.lang.ref.WeakReference
import kotlin.reflect.KProperty


/**
 * Copyright (C), 2018-2019, 享物说
 * FileName: Weak
 * Author: max
 * Date: 2019-06-13 22:02
 * Description: 弱引用，解决内存泄漏的问题
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */

class Weak<T : Any>(initializer: () -> T?) {

    private var weakReference = WeakReference<T?>(initializer())

    constructor():this({
        null
    })

    operator fun getValue(thisRef: Any?, property: KProperty<*>): T? {
        Log.d("Weak Delegate","-----------getValue")
        return weakReference.get()
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T?) {
        Log.d("Weak Delegate","-----------setValue")
        weakReference = WeakReference(value)
    }

}